package com.infore.airsupervise.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.MenuE;
import com.infore.airsupervise.utils.JumpUtil;

import java.util.List;

/**
 * 主界面GridView适配器
 *
 * @author: dingxinlin
 * @date: 2016-09-06 16:31
 */
public class ChartGridViewAdapter extends BaseAdapter {

    private List<MenuE> datas;
    private LayoutInflater layoutInflater;

    public ChartGridViewAdapter(Context ctx, List<MenuE> datas) {
        this.datas = datas;
        this.layoutInflater = LayoutInflater.from(ctx);
    }

    @Override
    public int getCount() {
        return datas==null ? 0 : datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_chart_grid_view, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final MenuE menu = datas.get(position);

        //设置内容
        holder.icon.setImageResource(menu.getIcon());
        holder.title.setText(menu.getTitle());

        // 设置点击事件
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JumpUtil.jumpFromTo(
                        v.getContext(), menu.getFirstActivity(), Constants.INTENT_OBJ_KEY, menu);
            }
        });

        return convertView;
    }

    class ViewHolder {
        public ImageView icon;
        public TextView title;

        public ViewHolder(View itemView) {
            this.icon = (ImageView) itemView.findViewById(R.id.menu_icon);
            this.title = (TextView) itemView.findViewById(R.id.menu_title);
        }
    }
}
